package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.enums.AttachCategoryEnumWaybill;
import cn.huiyunche.base.service.enums.AttachTypeEnum;
import cn.huiyunche.base.service.interfaces.SWaybillAttachService;
import cn.huiyunche.base.service.mappers.CWaybillAttachMapper;
import cn.huiyunche.base.service.mappers.CWaybillMapper;
import cn.huiyunche.base.service.model.CWaybill;
import cn.huiyunche.base.service.model.CWaybillAttach;
import cn.huiyunche.base.service.model.CWaybillAttachExample;
import cn.huiyunche.base.service.model.CWaybillExample;
import cn.huiyunche.base.service.vo.AttachVo;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SWaybillAttachServiceImpl implements SWaybillAttachService {

    @Autowired
    private CWaybillMapper cWaybillMapper;

    @Autowired
    private CWaybillAttachMapper waybillAttachMapper;

    @Override
    public Map<String, List<AttachVo>> getAttachs(Long id) {
        if (id == null || id == 0) {
            throw new BusinessException("订单id不能为空！");
        }

        Map<String, List<AttachVo>> map = new HashMap<>();

        CWaybillExample example = new CWaybillExample();
        example.createCriteria().andOrderIdEqualTo(id);
        List<CWaybill> list = cWaybillMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            throw new BusinessException("获取运单信息失败！");
        }
        CWaybill waybill = list.get(0);

        CWaybillAttachExample attachExample = new CWaybillAttachExample();
        attachExample.createCriteria().andWaybillIdEqualTo(waybill.getId());
        List<CWaybillAttach> attachs = waybillAttachMapper.selectByExample(attachExample);
        if (CollectionUtils.isNotEmpty(attachs)) {
            //发车附件
            List<AttachVo> sendPics = new ArrayList<>();
            //交车附件
            List<AttachVo> deliverPics = new ArrayList<>();
            CWaybillAttach attach = null;
            AttachVo vo = null;
            for (int i = 0; i < attachs.size(); i++) {
                attach = attachs.get(i);
                vo = new AttachVo();
                vo.setId(attach.getId());
                vo.setType(attach.getType());
                vo.setTypeText(AttachTypeEnum.getByValue(attach.getType()).getText());
                vo.setPicKey(attach.getPicKey());
                if (AttachCategoryEnumWaybill.SEND.getValue() == attach.getCategory()) {
                    sendPics.add(vo);
                }
                if (AttachCategoryEnumWaybill.DELIVER.getValue() == attach.getCategory()) {
                    deliverPics.add(vo);
                }
            }
            map.put("sendPics", sendPics);
            map.put("deliverPics", deliverPics);
        }

        return map;
    }

}
